package com.module.rotary.config;

import android.app.Application;

import com.dq.util.DateUtil;
import com.dq.util.SPUtil;
import com.dq.util.SystemUtil;
import com.module.rotary.entity.RotaryEntity;

import org.litepal.LitePal;

import java.util.List;

public class RotaryManager {

    private static volatile RotaryManager instance = null;

    public static RotaryManager getInstance() {
        if (instance == null) {
            synchronized (RotaryManager.class) {
                if (instance == null) {
                    instance = new RotaryManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     *
     * @param a
     */
    public void init(Application a) {
        SPUtil.getInstance(a);
        LitePal.initialize(a);
        if (!SystemUtil.isToday()) {//今天第1次打开
            SPUtil.setString("today", DateUtil.getDate(DateUtil.ymd, System.currentTimeMillis()));
            RotaryConfig.setLotteryNumber(10);//每天默认10次
        }
        addData();
    }

    /**
     * 添加奖品数据
     */
    private void addData() {
        if (getData().size() > 0) {
            return;
        }
        new RotaryEntity("华为mate40手机", "HUA", "WEI", "mate", "4", "0", false, false, false, false, false).save();
        new RotaryEntity("手机壳", "碎片1", "碎片2", "碎片3", "碎片4", "碎片5", false, false, false, false, false).save();
        new RotaryEntity("充值券", "碎片1", "碎片2", "碎片3", "碎片4", "碎片5", false, false, false, false, false).save();
        new RotaryEntity("华为蓝牙耳机", "碎片1", "碎片2", "碎片3", "碎片4", "碎片5", false, false, false, false, false).save();
        new RotaryEntity("蓝牙音箱", "碎片1", "碎片2", "碎片3", "碎片4", "碎片5", false, false, false, false, false).save();
    }

    public List<RotaryEntity> getData() {
        return LitePal.findAll(RotaryEntity.class);
    }
}